import pandas as pd
import numpy as np

from pyecharts import options as opts
from pyecharts.charts import Geo, Page, Map, Pie, Bar, WordCloud
from pyecharts.globals import ChartType, SymbolType


class Collector:
    charts = []

    @staticmethod
    def funcs(fn):
        Collector.charts.append((fn, fn.__name__))


C = Collector()

data = [('googleapis.com', 79979),
        ('qq.com', 25716),
        ('bilibili.com', 11771),
        ('qpic.cn', 5735),
        ('google.com', 5207),
        ('amap.com', 3749),
        ('hdslb.com', 3693),
        ('zhihu.com', 2994),
        ('163.com', 2042),
        ('sogou.com', 1952),
        ('alipay.com', 1364),
        ('snssdk.com', 1317),
        ('meituan.com', 1298),
        ('p2cdn.com', 1287),
        ('baidu.com', 1274),
        ('other', 25598)]

data.reverse()

data_night = [('qq.com', 588),
              ('zhihu.com', 191),
              ('qpic.cn', 131),
              ('google.com', 120),
              ('sogou.com', 96),
              ('xiaojukeji.com', 75),
              ('amap.com', 59),
              ('qunar.com', 52),
              ('baidu.com', 47),
              ('com.cn', 36),
              ('qlogo.cn', 36),
              ('zhimg.com', 31),
              ('163.com', 30),
              ('didistatic.com', 16),
              ('vzuu.com', 16),
              ('jpush.cn', 15),
              ('crashlytics.com', 15),
              ('xdrig.com', 13),
              ('v2ex.com', 12),
              ('oneplus.cn', 11),
              ('bdstatic.com', 11),
              ('aliyuncs.com', 11),
              ('gstatic.com', 10),
              ('sinaimg.cn', 10),
              ('shuzilm.cn', 10),
              ('dida365.com', 9),
              ('biliapi.com', 8),
              ('imgur.com', 8),
              ('udache.com', 7),
              ('bilibili.com', 7)]
data_night.reverse()

data_cloud = [('googleapis', 79979),
              ('qq', 25716),
              ('bilibili', 11771),
              ('qpic', 5735),
              ('google', 5207),
              ('amap', 3749),
              ('hdslb', 3693),
              ('zhihu', 2994),
              ('163', 2042),
              ('sogou', 1952),
              ('alipay', 1364),
              ('snssdk', 1317),
              ('meituan', 1298),
              ('p2cdn', 1287),
              ('baidu', 1274),
              ('iqiyi', 1131),
              ('mobike', 1059),
              ('qlogo', 1016),
              ('biliapi', 955),
              ('acgvideo', 838),
              ('gstatic', 811),
              ('biliapi.net', 808),
              ('alicdn', 678),
              ('qy.net', 666),
              ('juejin.im', 650),
              ('com', 640),
              ('taobao', 610),
              ('huodongxing', 605),
              ('xycdn', 588),
              ('qunar', 583),
              ('jpush', 574),
              ('byteimg', 549),
              ('126.net', 515),
              ('bdurl.net', 482),
              ('xiaojukeji', 466),
              ('meituan.net', 462),
              ('dianping', 448),
              ('netease', 447),
              ('oppomobile', 389),
              ('10010', 379),
              ('alipayobjects', 358),
              ('amemv', 331),
              ('umeng', 318),
              ('dida365', 280),
              ('xdrig', 248),
              ('zhimg', 243),
              ('csg', 238),
              ('oneplus', 235),
              ('bytecdn', 233),
              ('xitu.io', 221),
              ('pstatp', 178),
              ('thinkhard.tech', 174),
              ('bdstatic', 166),
              ('weathercn', 164),
              ('aliyuncs', 160),
              ('teddymobile', 157),
              ('iqiyipic', 144),
              ('didistatic', 133),
              ('gvt2', 131),
              ('coolapk', 128),
              ('qchannel03', 124),
              ('nocode', 119),
              ('biligame', 117),
              ('crashlytics', 115),
              ('doubleclick.net', 113),
              ('google-analytics', 110),
              ('imgur', 110),
              ('ele.me', 105),
              ('aocde', 92),
              ('jd', 89),
              ('v2ex', 89),
              ('app-measurement', 88),
              ('sogoucdn', 87),
              ('tencent', 82),
              ('uc', 76),
              ('qinengkeji', 73),
              ('com.hk', 69),
              ('dpfile', 68),
              ('getui', 67),
              ('udache', 67),
              ('just4fun.site', 65),
              ('qunarzz', 64),
              ('leancloud', 58),
              ('lehuipay', 58),
              ('sinaimg', 57),
              ('bcebos', 54),
              ('githubusercontent', 53),
              ('irs01', 52),
              ('gtimg', 52),
              ('vzuu', 52),
              ('cnzz', 50),
              ('aliapp.org', 50),
              ('meijuniao.net', 49),
              ('shuzilm', 46),
              ('shouqianba', 45),
              ('unpkg', 45),
              ('growingio', 44),
              ('cmpassport', 42),
              ('h2os', 42),
              ('qiyi', 41),
              ('gtimg', 41),
              ('bdimg', 40),
              ('tmall', 40),
              ('autonavi', 39),
              ('mob', 39),
              ('avoscloud', 36),
              ('sf-express', 35),
              ('mmstat', 35),
              ('googlesyndication', 34),
              ('qqmail', 33),
              ('douban', 33),
              ('huoxing24', 33),
              ('ez4q2', 32),
              ('bsgslb', 32),
              ('51y5.net', 32),
              ('12306', 31),
              ('appsflyer', 31),
              ('ifengimg', 31),
              ('no4book', 31),
              ('googletagmanager', 29),
              ('jianshu', 28),
              ('shumafen', 28),
              ('facebook', 27),
              ('mydrivers', 27),
              ('xiaomi.net', 26),
              ('geekbang.org', 26),
              ('umengcloud', 25),
              ('myqcloud', 25),
              ('smzdm', 25),
              ('wjx.top', 24),
              ('github', 24),
              ('wm-motor', 24),
              ('didialift', 23),
              ('36kr', 23),
              ('codelab.club', 22),
              ('wezhuiyi', 22),
              ('ucweb', 22),
              ('toutiao', 22),
              ('cmfspay', 20),
              ('bootcss', 20),
              ('meizu', 19),
              ('toutiaocdn', 18),
              ('lmgtfy', 18),
              ('edu', 18),
              ('neixin', 17),
              ('xuebuyuan', 17),
              ('umsns', 17),
              ('elemecdn', 17),
              ('360buyimg', 17),
              ('feedblitz', 16),
              ('beijing-time.org', 15),
              ('servicewechat', 14),
              ('163yun', 14),
              ('cnblogs', 14),
              ('optimix.asia', 14),
              ('flyhand', 13),
              ('meiguanjia.net', 13),
              ('71edge', 13),
              ('luckincoffee', 13),
              ('xiumi.us', 12),
              ('githubassets', 12),
              ('qbox.me', 11),
              ('lejuhub', 11),
              ('csdnimg', 11),
              ('jomodns', 11),
              ('aliyun', 11),
              ('izatcloud.net', 11),
              ('qmlog', 11),
              ('myapp', 11),
              ('appinn.net', 11),
              ('weixinbridge', 10),
              ('1plus.io', 10),
              ('csdn.net', 10),
              ('qchannel01', 9),
              ('miaozhen', 9),
              ('weibo', 9),
              ('wangbase', 9),
              ('clmbtech', 9),
              ('youzan', 9),
              ('myoppo', 9),
              ('wojiazongguan', 9),
              ('360', 9),
              ('ifeng', 9),
              ('eqxiu', 8),
              ('openspeech', 8),
              ('127.net', 7),
              ('shimo.im', 7),
              ('typekit.net', 7),
              ('tanx', 7),
              ('url', 7),
              ('wm-imotor', 7),
              ('sharesmile', 7),
              ('leanplum', 7),
              ('idqqimg', 7),
              ('baidustatic', 7),
              ('iteye', 7),
              ('alibaba', 7),
              ('lkme.cc', 7),
              ('loli.net', 7),
              ('appjiagu', 7),
              ('chaihuo.org', 6),
              ('iprchn', 6),
              ('36krcnd', 6),
              ('ctrip', 6),
              ('qunarcdn', 6),
              ('disqus', 5),
              ('cytcm', 5),
              ('kuyun88', 5),
              ('mozilla', 5),
              ('3', 5),
              ('googletagservices', 5),
              ('googleadsserving', 5),
              ('ruanyifeng', 5),
              ('smartont.net', 4),
              ('gosmarthome', 4),
              ('bshare', 4),
              ('qiniucdn', 4),
              ('1688', 4),
              ('mediav', 4),
              ('mxplay', 4),
              ('ixiguavideo', 4),
              ('t', 4),
              ('189', 4),
              ('openinstall.io', 4),
              ('lyhanda', 4),
              ('optimix', 4),
              ('j2inter', 3),
              ('tv1box', 3),
              ('utteranc.es', 3),
              ('sankuai', 3),
              ('hdxu', 3),
              ('tddmp', 3),
              ('miaomiaoz', 3),
              ('appinn', 3),
              ('zdmimg', 3),
              ('inmobi', 3),
              ('youtube', 3),
              ('qcloud', 3),
              ('wm-motor', 3),
              ('mathjax.org', 3),
              ('taboola', 3),
              ('smcdn', 3),
              ('xiaomi', 3),
              ('youzanyun', 3),
              ('igexin', 3),
              ('qcloudimg', 3),
              ('gvt1', 2),
              ('weiyun', 2),
              ('tuyacn', 2),
              ('gemius.pl', 2),
              ('geektutu', 2),
              ('fugetech', 2),
              ('fastapi.net', 2),
              ('v2ex.co', 2),
              ('david-smith.org', 2),
              ('wjx', 2),
              ('chengzijianzhan', 2),
              ('wosaimg', 2),
              ('wzrkt', 2),
              ('xf-yun', 2),
              ('youku', 2),
              ('yunfengdie', 2),
              ('baifubao', 2),
              ('sohu', 2),
              ('adnxs', 2),
              ('52ecy', 2),
              ('rokid', 2),
              ('miaomiaozhe', 2),
              ('mzstatic', 2),
              ('adsrvr.org', 2),
              ('oneplusmobile', 2),
              ('adget', 2),
              ('lncld.net', 2),
              ('qipus', 2),
              ('jinshuju.net', 2),
              ('reachmax', 2),
              ('ming.today', 2),
              ('sensorsdata', 1),
              ('boip.net', 1),
              ('pinduoduo', 1),
              ('wosai', 1),
              ('inmobi', 1),
              ('mathtag', 1),
              ('highcharts', 1),
              ('advertising', 1),
              ('impact-ad.jp', 1),
              ('netease.im', 1),
              ('callget', 1),
              ('bttrack', 1),
              ('adjust', 1),
              ('mfadsrvr', 1),
              ('lnk0', 1),
              ('yahoo', 1),
              ('ydstatic', 1),
              ('snowballtech', 1),
              ('mh163k', 1),
              ('bidswitch.net', 1),
              ('svend.cc', 1),
              ('yqwyx.xyz', 1),
              ('mlinks.cc', 1),
              ('mozilla.org', 1),
              ('zenmxapps', 1),
              ('mozilla.net', 1),
              ('placeholder', 1),
              ('cloudflare', 1),
              ('scorecardresearch', 1),
              ('extraimage.net', 1),
              ('sc.gg', 1),
              ('ros.org', 1),
              ('guazi', 1),
              ('gio.ren', 1),
              ('testin', 1),
              ('geetest', 1),
              ('jiguang', 1),
              ('ugdtimg', 1),
              ('futunn', 1),
              ('jaeapp', 1),
              ('36krcdn', 1),
              ('duxiaoman', 1),
              ('shimodev', 1),
              ('akamaized.net', 1),
              ('jsdelivr.net', 1),
              ('dripemail2', 1),
              ('qnssl', 1),
              ('dingtalk', 1),
              ('aflink', 1),
              ('storygize.net', 1),
              ('kuaidadi', 1),
              ('shadowsocks.org', 1),
              ('qhres', 1),
              ('qhimg', 1),
              ('ictr', 1)]


@C.funcs
def pie_base() -> Pie:
    c = (
        Pie()
            .add("", data)
            .set_global_opts(title_opts=opts.TitleOpts(title=""))
            .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}", font_size=18))
    )
    return c


@C.funcs
def bar_base() -> Bar:
    c = (
        Bar(init_opts=opts.InitOpts(width="100%", height="800%"))
            .add_xaxis(xaxis_data=[x[0] for x in data])
            .add_yaxis(series_name="所有公司排行",
                       color='#59a2a7',
                       yaxis_data=[x[1] for x in data])
            .reversal_axis()
            .set_series_opts(label_opts=opts.LabelOpts(position="right"))

    )
    return c


@C.funcs
def bar_base() -> Bar:
    c = (
        Bar(init_opts=opts.InitOpts(width="100%", height="800%"))
            .add_xaxis(xaxis_data=[x[0] for x in data_night])
            .add_yaxis(series_name="夜间活动排行",
                       color='black',
                       yaxis_data=[x[1] for x in data_night])
            .reversal_axis()
            .set_series_opts(label_opts=opts.LabelOpts(position="right"))

    )
    return c


@C.funcs
def wordcloud_base() -> WordCloud:
    c = (
        WordCloud()
            .add("", data_cloud[1:], word_size_range=[12, 100], shape=SymbolType.DIAMOND)
            .set_global_opts(title_opts=opts.TitleOpts(title="词云"))
    )
    return c


data_oppo = [('epoch.cdo.oppomobile.com', 208),
             ('gslb.cdo.oppomobile.com', 38),
             ('istore.oppomobile.com', 38),
             ('opsapi.store.oppomobile.com', 34),
             ('api.cdo.oppomobile.com', 22),
             ('message.pull.oppomobile.com', 21),
             ('st.pull.oppomobile.com', 13),
             ('cdopic0.oppomobile.com', 9),
             ('newds01.myoppo.com', 9),
             ('httpdns.push.oppomobile.com', 4),
             ('conn1.oppomobile.com', 1),
             ('iopen.cdo.oppomobile.com', 1)]
data_oppo.reverse()

@C.funcs
def bar_base() -> Bar:
    c = (
        Bar(init_opts=opts.InitOpts(width="100%", height="800%"))
            .add_xaxis(xaxis_data=[x[0] for x in data_oppo])
            .add_yaxis(series_name="oneplus and oppo",
                       color='#009363',
                       yaxis_data=[x[1] for x in data_oppo])
            .reversal_axis()
            .set_series_opts(label_opts=opts.LabelOpts(position="right"))

    )
    return c


Page().add(*[fn() for fn, _ in C.charts]).render('./output/map_bar_company.html')
